package com.oasys.config;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import com.oasys.enums.OaResponseCode;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import org.springframework.web.bind.annotation.RequestMethod;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.builders.ResponseMessageBuilder;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.ResponseMessage;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: TODO
 * @Author: mis_wu
 * @Date: 2022/5/12
 * 开启knif4j
 * 开启Swagger
 **/
@Configuration
@EnableSwagger2
@EnableKnife4j
public class SwaggerConfig {
    /**
     * 配置Swagger的bean实例
     * @param environment env
     * @return doc
     */
    @Bean
    public Docket docket(Environment environment) {

        List<ResponseMessage> responseMessageList = new ArrayList<>();
        Arrays.stream(OaResponseCode.values()).forEach(stateCodeEnum ->
        {
            responseMessageList.add(
                    //这种形式swagger提示自定义返回msg找不到
                    new ResponseMessageBuilder()
                            .code(stateCodeEnum.getCode())
                            .message(stateCodeEnum.getMsg())
                            .responseModel(new ModelRef(stateCodeEnum.getMsg()))
                            .build());
        });
        //设置要显示的Swagger环境
        Profiles profiles = Profiles.of("dev");
        //获取项目环境：是生产环境还是发布环境
        boolean flag = environment.acceptsProfiles(profiles);
        return new Docket(DocumentationType.SWAGGER_2)
                //添加全局状态码
                .globalResponseMessage(RequestMethod.GET, responseMessageList)
                .globalResponseMessage(RequestMethod.POST, responseMessageList)
                .globalResponseMessage(RequestMethod.PUT, responseMessageList)
                .globalResponseMessage(RequestMethod.DELETE, responseMessageList)
                .apiInfo(apiInfo())
                //是否启用swagger，如果为false则swagger不能再浏览器中访问
                .groupName("os-sys-API")
                .enable(flag)
                //通过select()方法配置扫描接口,RequestHandlerSelectors配置如何扫描接口
                .select()
                //指定扫描的api包
                .apis(RequestHandlerSelectors.basePackage("com.oasys.controller"))
                //any()：任何请求都扫描  ant()通过paths()方法配置扫描接口,PathSelectors配置如何扫描接口 "/sys/**"
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        //链式编程,固定
        return new ApiInfoBuilder()
                .title("oa管理系统接口文档")
                .description("os-system RestFul APIS")
                .contact(new Contact("mis_wu", "https://www.baidu.com","mis_wu1998@163.com"))
                .license("Apache 2.0 开源许可证")
                .version("1.0.0")
                .build();
    }
}
